package cn.gdcp.lkqapp07;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final int GET_IMG = 1001;
    private static String TAG = "MainActivity";
    /*private String strUrl = "https://i0.hdslb.com/bfs/album/1eab364136f7dc024eac1d663bb843c43c996798.jpg";*/
    private String strUrl = "https://i0.hdslb.com/bfs/album/d5e0d8f9b1eeff8d07d0a2300ac3c99b8e9059b7.jpg";
    private NetworkImageView netImg;
    private ImageView imageView;
    private Button btnHttp;
    private Button btnImageRequest;
    private Button btnImageLoader;
    private Button btnNet;
    private Handler handler;
    private RequestQueue requestQueue;
    private MyImageCache myImageCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue = Volley.newRequestQueue(MainActivity.this);
        myImageCache = new MyImageCache();
        btnHttp = findViewById(R.id.btn_http);
        btnImageRequest = findViewById(R.id.btn_imageRequest);
        btnImageLoader = findViewById(R.id.btn_imageLoader);
        btnNet = findViewById(R.id.btn_net);
        imageView = findViewById(R.id.iv_img);
        netImg = findViewById(R.id.net_img);


        btnNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageLoader imageLoader = new ImageLoader(requestQueue,myImageCache);
                netImg.setDefaultImageResId(R.drawable.moren);
                netImg.setErrorImageResId(R.drawable.cuowu);
                netImg.setImageUrl(strUrl,imageLoader);
            }
        });

        btnImageLoader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageLoader imageLoader = new ImageLoader(requestQueue,myImageCache);
                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(
                        imageView, R.drawable.moren,R.drawable.cuowu);
                imageLoader.get(strUrl,imageListener);
            }
        });


        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == GET_IMG){
                    Bitmap bitmap = (Bitmap)msg.obj;
                    imageView.setImageBitmap(bitmap);
                }
                return false;
            }
        });

        btnImageRequest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageRequest imageRequest = new ImageRequest(strUrl, new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        imageView.setImageBitmap(bitmap);
                    }
                }, 0, 0, Bitmap.Config.ARGB_4444, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });
                requestQueue.add(imageRequest);
            }
        });

        btnHttp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL(strUrl);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            Message message = Message.obtain();
                            message.what = GET_IMG;
                            message.obj = bitmap;
                            handler.sendMessage(message);

                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

    }
}
